﻿var CurrentArea = {};
var CurrentEquipment = {};
Ext.onReady(function () {
    PageSafe();

    //广告
    var adstore1 = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Advertisement.AdvertisementView', 'GetAdvertisement'),
        fields: ['PID', 'GName'],
        root: 'ResponseObject'
    });
    adstore1.load();
    //设备信息
    var equipmentstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'GetEquipmentList'),
        root: 'ResponseObject',
        fields: ['PID', 'EquipmentName', 'EquipmentCode', 'EquipmentIP', 'AreaID', 'PersonID', 'NTime'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.ID = CurrentArea.id;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var equipmentsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = equipmentgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentEquipment = k[0].json;
                    adstore.load();
                }
            }
        }
    });
    var equipmentgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 550,
        sm: equipmentsm,
        loadMask: true, //加载
        columns: [
       equipmentsm,
       { id: 'EquipmentName', header: '设备名称', width: 100, dataIndex: 'EquipmentName', sortable: true },
       { id: 'EquipmentCode', header: '设备ID', width: 70, dataIndex: 'EquipmentCode', sortable: true },
       { id: 'EquipmentIP', header: '设备IP', width: 70, dataIndex: 'EquipmentIP', sortable: true },
       { id: 'NTime', header: '时间', width: 100, dataIndex: 'NTime', sortable: true }
   ],
        store: equipmentstore
    });
    //广告信息
    var adstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Advertisement.AdvertisementView', 'GetBindAd'),
        root: 'ResponseObject',
        fields: ['PID', 'AdvertisementID', 'CurrentAd.GName', 'CurrentAd.Period', 'CurrentAd.SurplusTime', 'Creater', 'EquipmentID', 'ETime', 'FTime', 'Lastupdate', 'F_Time', 'E_Time', 'NTime'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.ID = CurrentEquipment.PID;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var adsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = equipmentgrid.getSelectionModel().selections.items;
            }
        }
    });
    var adgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 550,
        sm: adsm,
        loadMask: true, //加载
        columns: [
       adsm,
       { id: 'F_Time', header: '开始时间', width: 70, dataIndex: 'F_Time', sortable: true },
       { id: 'E_Time', header: '结束时间', width: 70, dataIndex: 'E_Time', sortable: true },
       { id: 'CurrentAd.GName', header: '广告名称', width: 70, dataIndex: 'CurrentAd.GName', sortable: true },
       { id: 'CurrentAd.Period', header: '有效期限', width: 70, dataIndex: 'CurrentAd.Period', sortable: true },
       { id: 'CurrentAd.SurplusTime', header: '剩余时间', width: 70, dataIndex: 'CurrentAd.SurplusTime', sortable: true },
       { id: 'Creater', header: '发布人', width: 70, dataIndex: 'Creater', sortable: true },
       { id: 'NTime', header: '发布时间', width: 70, dataIndex: 'NTime', sortable: true },
       { id: '', header: '预览', width: 50, dataIndex: '', sortable: true }
   ],
        store: adstore
    });

    var AreaTree = new Ext.tree.TreePanel({
        width: 260,
        style: 'padding-left:20px',
        border: false,
        height: 530,
        split: true,
        rootVisible: true,
        useArrows: false, /*去掉树线*/
        autoScroll: true,
        collapseMode: 'mini',
        root: {},
        listeners: {
            render: function () {
                renderTree(AreaTree); /*渲染菜单树*/
            },
            checkchange: function (node, state) {
                if (state) {
                    CurrentArea = node.attributes;
                    equipmentstore.load();
                }
            }
        }
    });


    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'west',
            style: 'padding:2px;',
            title: '地区列表',
            iconCls: 'arealist-icon',
            split: true,
            items: AreaTree,
            width: 200
        }, {
            region: 'center',
            layout: 'border',
            border: false,
            items: [{
                region: 'west',
                width: 360,
                style: 'padding:2px;',
                iconCls: 'equipment-icon',
                title: '设备管理',
                split: true,
                items: equipmentgrid
            }, {
                region: 'center',
                style: 'padding:2px;',
                iconCls: 'gg-icon',
                tbar: [{ text: '绑定设备广告', iconCls: 'add-icon', handler: function () {
                    if (CurrentArea.id == null && CurrentEquipment.PID == null) {
                        Ext.Msg.alert("缺少项提示", "请选择要绑定广告投放的地区或者设备");
                        return;
                    }
                    var adwin = new Ext.Window({
                        height: 180,
                        width: 400,
                        title: '广告投放',
                        resizable: false,
                        iconCls: 'machine-icon',
                        modal: true,
                        layout: 'table',
                        bodyStyle: 'padding: 10px',
                        defaults: {
                            // 对每一个子面板都有效applied to each contained panel
                            bodyStyle: 'padding:10px'
                        },
                        layoutConfig: {
                            // 这里指定总列数The total column count must be specified here
                            columns: 8
                        },
                        items: [{
                            xtype: 'label',
                            width: 120,
                            text: '开始时间：'
                        }, {
                            xtype: 'datefield',
                            width: 120,
                            id: 'F_Time'
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '时'
                        }, {
                            xtype: 'textfield',
                            id: 'F_Hour',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '分'
                        }, {
                            xtype: 'textfield',
                            id: 'F_Min',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '秒'
                        }, {
                            xtype: 'textfield',
                            id: 'F_Sec',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 120,
                            text: '结束时间：'
                        }, {
                            xtype: 'datefield',
                            width: 120,
                            id: 'E_Time'
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '时'
                        }, {
                            xtype: 'textfield',
                            id: 'E_Hour',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '分'
                        }, {
                            xtype: 'textfield',
                            id: 'E_Min',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 50,
                            text: '秒'
                        }, {
                            xtype: 'textfield',
                            id: 'E_Sec',
                            width: 50
                        }, {
                            xtype: 'label',
                            width: 120,
                            text: '广告：'
                        }, {
                            width: 150,
                            colspan: 7,
                            id: 'AdName',
                            xtype: 'combo',
                            displayField: 'GName',
                            valueField: 'PID',
                            selectOnFocus: true,
                            triggerAction: 'all',
                            store: adstore1
                        }],
                        buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                            var re = new JsonRequest();
                            re.params.ftime = Ext.getCmp("F_Time").getValue();
                            re.params.fh = Ext.getCmp("F_Hour").getValue();
                            re.params.fm = Ext.getCmp("F_Min").getValue();
                            re.params.fs = Ext.getCmp("F_Sec").getValue();
                            re.params.etime = Ext.getCmp("E_Time").getValue();
                            re.params.eh = Ext.getCmp("E_Hour").getValue();
                            re.params.em = Ext.getCmp("E_Min").getValue();
                            re.params.es = Ext.getCmp("E_Sec").getValue();
                            re.params.ad = Ext.getCmp("AdName").getValue();
                            re.params.ID = CurrentEquipment.PID;

                            Ext.Ajax.request({
                                url: Ext.getClassUrl('Advertisement.AdvertisementView', 'SaveBindAd_Equipment'),
                                jsonData: re,
                                success: function (re, op) {
                                    var obj = Ext.decode(re.responseText);
                                    if (obj.ResponseObject != null) {
                                        Ext.Msg.alert("操作提示", "设备绑定广告成功");
                                        adstore.load();
                                    }
                                    else {
                                        Ext.Msg.alert("操作提示", "设备绑定广告失败，请联系管理员");
                                    }
                                    adwin.close();
                                }
                            });
                        }
                        }, { text: '取消', iconCls: 'cancel-icon', handler: function () { adwin.close(); } }],
                        listeners: {
                            render: function () {
                                if (CurrentArea.id != null) {
                                    //Ext.getCmp("E_Address").setValue(CurrentArea.text);
                                }
                            }
                        }
                    }).show();
                }
                }, { text: '修改绑定设备广告', iconCls: 'update-icon' }, { text: '删除绑定设备广告', iconCls: 'del-icon'}],
                title: '设备当前广告',
                items: adgrid
            }]
        }]
    });
});

//初始化树
var renderTree = function (tree) {
    var re = new JsonRequest();
    re.category = CATEGORY.READONLY;
    re.params.param = {};
    Ext.Ajax.request({
        url: Ext.getClassUrl('BaseManager.PersonManager.AreaView', 'AreaData'),
        jsonData: re,
        success: function (re, op) {
            var obj = Ext.decode(re.responseText).ResponseObject;
            var ns = {}, f = tree;
            ns.Children = obj;
            var r = new Ext.tree.TreeNode({
                id: '0',
                text: '全部地区'
            });
            var selectedNode = null, selectedId = f.selectedId;
            function appendChild(node, o) {
                if (node.PID == selectedId) {
                    selectedNode = node;
                }
                if (o.Children != null && o.Children.length > 0) {
                    for (var a = 0; a < o.Children.length; a++) {
                        var n = new Ext.tree.TreeNode({
                            id: o.Children[a].PID,
                            icon: '../images/menu/ui-text-area.png',
                            text: o.Children[a].AreaName,
                            parentId: o.Children[a].ParentID,
                            AreaRemark: o.Children[a].AreaRemark,
                            PersonID: o.Children[a].PersonID,
                            checked: false
                        });
                        node.appendChild(n);
                        appendChild(n, o.Children[a]);

                    }
                }
            }
            appendChild(r, ns);
            f.setRootNode(r);

            tree.registerNode(r); /*注册节点, 不然getNodeById(node.id)找不到这个节点*/
            tree.root.expand(true);
        }
    })
}